//
//  AudioPlayerViewController.swift
//  funcode
//
//  Created by Wan Jiading on 2017/2/6.
//  Copyright © 2017年 WanJiading. All rights reserved.
//

import UIKit
import AVFoundation

class AudioPlayerViewController: UIViewController {

	var currentLengthLabel = UILabel()
	var maxLengthLabel = UILabel()
	
	
	var showView: UIView!
	
    override func viewDidLoad() {
        super.viewDidLoad()
		
		setupSubViews()
		setupPlayer()
    }
	
	private func setupPlayer() {
		
		
		
//		let urlString = "http://exploiter.oss-cn-beijing.aliyuncs.com/audio/147/5p2h57q56bG85b6X5pWR5LqG_chinesestory_1484588477_1.mp3"
//		JDPlayer.shared.play(URL(string: urlString)!, self.showView)
	}
	
	private func setupSubViews() {
		
		self.view.backgroundColor = UIColor.white
		
		let screenSize = UIScreen.main.bounds
		self.showView = UIView(frame: CGRect(x: 0, y: 64, width: screenSize.width, height: screenSize.height - 64))
		self.view.addSubview(self.showView)

		
//		let x = 30
//		let y = 200
//		let w = 300
//		let h = 50
		
//		setupLabel(currentLengthLabel, "下载大小")
//		currentLengthLabel.frame = CGRect(x: x, y: y, width: w, height: h)
//		currentLengthLabel.sizeToFit()
//		self.view.addSubview(currentLengthLabel)
//		
//		setupLabel(maxLengthLabel, "总大小")
//		maxLengthLabel.frame = CGRect(x: x, y: Int(currentLengthLabel.frame.maxY + 20), width: w, height: h)
//		maxLengthLabel.sizeToFit()
//		self.view.addSubview(maxLengthLabel)
		
		
		
		let btn1 = self.getButton("音频1", height: 100, action: #selector(self.buttonClick1))
		self.view.addSubview(btn1)
		let btn2 = self.getButton("音频2", height: 160, action: #selector(self.buttonClick2))
		self.view.addSubview(btn2)
		let btn3 = self.getButton("音频3", height: 220, action: #selector(self.buttonClick3))
		self.view.addSubview(btn3)
		let btn4 = self.getButton("音频4", height: 280, action: #selector(self.buttonClick4))
		self.view.addSubview(btn4)
		let btn5 = self.getButton("音频5", height: 340, action: #selector(self.buttonClick5))
		self.view.addSubview(btn5)
		
	}
	
	
	func buttonClick1() {
		let urlString = "http://exploiter.oss-cn-beijing.aliyuncs.com/audio/147/5p2h57q56bG85b6X5pWR5LqG_chinesestory_1484588477_1.mp3"
		JDPlayer.shared.play(URL(string: urlString)!, self.showView)
	}
	
	func buttonClick2() {
		let urlString = "http://exploiter.oss-cn-beijing.aliyuncs.com/audio/12/VGltZSBGb3IgQmVk_music_1484588342_3.mp3"
		JDPlayer.shared.play(URL(string: urlString)!, self.showView)
	}
	
	func buttonClick3() {
		let urlString = "http://exploiter.oss-cn-beijing.aliyuncs.com/audio/147/5p2h57q56bG85b6X5pWR5LqG_chinesestory_1484588477_1.mp3"
		JDPlayer.shared.play(URL(string: urlString)!, self.showView)
	}
	
	func buttonClick4() {
		let urlString = "http://exploiter.oss-cn-beijing.aliyuncs.com/audio/12/VGltZSBGb3IgQmVk_music_1484588342_3.mp3"
		JDPlayer.shared.play(URL(string: urlString)!, self.showView)
	}
	
	func buttonClick5() {
		let urlString = "http://exploiter.oss-cn-beijing.aliyuncs.com/audio/147/5p2h57q56bG85b6X5pWR5LqG_chinesestory_1484588477_1.mp3"
		JDPlayer.shared.play(URL(string: urlString)!, self.showView)
	}
	
	private func setupLabel(_ label: UILabel, _ text: String) {
		
		label.text = text
		label.font = UIFont.systemFont(ofSize: 16)
		label.textAlignment = .left
		label.textColor = UIColor.red
		label.backgroundColor = UIColor.white
		
	}
	
	func getButton(_ title: String, height: CGFloat, action: Selector) -> UIButton {
		let button = UIButton(type: .custom)
		button.frame = CGRect(x: CGFloat(100), y: height, width: CGFloat(100), height: CGFloat(50))
		button.setTitle(title, for: .normal)
		button.backgroundColor = UIColor.darkGray
		button.setTitleColor(UIColor.black, for: .normal)
		button.addTarget(self, action: action, for: .touchUpInside)
		return button
	}
}
